package com.example.trans.controller;

import com.example.trans.model.UserInfo;
import com.example.trans.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/userInfo")
public class UserInfoController {
    @Autowired
    private DataSourceTransactionManager dataSourceTransactionManager;
    @Autowired
    private TransactionDefinition transactionDefinition;
    @Autowired
    private UserInfoService userInfoService;
    @RequestMapping("/register")
    public Boolean register(String userName, String password){
        //开启事务
        TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
        log.info("获取到参数userName: {}, password: {}", userName, password);
        Integer result = userInfoService.insertUserInfo(userName, password);
        System.out.println("插入结果, result: " + result);
//        if(result > 0){
//            return true;
//        }
//        回滚事务
        dataSourceTransactionManager.rollback(transactionStatus);
        //提交事务
//        dataSourceTransactionManager.commit(transactionStatus);
        return true;
    }
}
